package com.fotkidemo.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created with IntelliJ IDEA.
 * User: roma
 * Date: 6/27/12
 * Time: 12:56 AM
 * To change this template use File | Settings | File Templates.
 */

/**
 * Статические методы для работы с аутентификационными данными
 * (сохранение, получение access_token, сохранение, получение логина пользователя)
 */
public class AuthUtil {
    public static final String OAUTH_API_PREFERENCES_ID = "com.fotkidemo.oauth";
    public static final String ACCESS_TOKEN = "access_token";
    public static final String LOGIN_NAME = "login_name";

    public static void saveAccessToken(Context context, String access_token) {
        final SharedPreferences storage = context.getSharedPreferences(OAUTH_API_PREFERENCES_ID, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = storage.edit();
        editor.putString(ACCESS_TOKEN, access_token);
        editor.commit();
    }

    public static void deleteAccessToken(Context context) {
        final SharedPreferences storage = context.getSharedPreferences(OAUTH_API_PREFERENCES_ID, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = storage.edit();
        editor.remove(ACCESS_TOKEN);
        editor.commit();
    }

    public static String getAccessToken(Context context) {
        final SharedPreferences storage = context.getSharedPreferences(OAUTH_API_PREFERENCES_ID, Context.MODE_PRIVATE);
        String access_token = storage.getString(ACCESS_TOKEN, null);
        return access_token;
    }

    public static boolean checkAccessToken(Context context) {
        return (getAccessToken(context) != null && getLoginName(context) != null);
    }

    public static void saveLoginName(Context context, String login) {
        final SharedPreferences storage = context.getSharedPreferences(OAUTH_API_PREFERENCES_ID, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = storage.edit();
        editor.putString(LOGIN_NAME, login);
        editor.commit();
    }

    public static void deleteLoginName(Context context) {
        final SharedPreferences storage = context.getSharedPreferences(OAUTH_API_PREFERENCES_ID, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = storage.edit();
        editor.remove(LOGIN_NAME);
        editor.commit();
    }

    public static String getLoginName(Context context) {
        final SharedPreferences storage = context.getSharedPreferences(OAUTH_API_PREFERENCES_ID, Context.MODE_PRIVATE);
        return storage.getString(LOGIN_NAME, null);

    }
}
